package com.xusiyan08.controller;

import com.xusiyan08.entity.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@Component
public class ItemFeignClientFallback implements ItemFeignClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(ItemFeignClientFallback.class);

    @Override
    public ResponseEntity<Item> getItemById(Long itemId) {
        LOGGER.error("Fallback: getItemById called for itemId {}. Service might be down or timed out.", itemId);
        
        Item fallbackItem = new Item();
        fallbackItem.setItemId(itemId);
        fallbackItem.setTitle("商品信息加载失败");
        fallbackItem.setDescription("商品信息加载中，请稍后重试");
        // Simulating -1 stock as per requirement
        // Note: The 'stock' field is not in the Item entity, assuming it's part of the frontend logic or another DTO.
        // We will set a field that is present, like viewCount, to -1 to signify this.
        fallbackItem.setViewCount(-1);
        fallbackItem.setDegraded(true); // Set the degraded flag

        return ResponseEntity.status(HttpStatus.OK).body(fallbackItem);
    }
}
